
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;
namespace Tathata.Web.Administration
{
    public partial class Administration_ManufacturerAdd : BaseNopAdministrationPage
    {       
        private void FillDropDowns()
        {
            this.ddlTemplate.Items.Clear();
            ManufacturerTemplateCollection manufacturerTemplates = ManufacturerTemplateManager.GetAllManufacturerTemplates();
            foreach (ManufacturerTemplate manufacturerTemplate in manufacturerTemplates)
            {
                ListItem item2 = new ListItem(manufacturerTemplate.Name, manufacturerTemplate.ManufacturerTemplateID.ToString());
                this.ddlTemplate.Items.Add(item2);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.FillDropDowns();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Picture manufacturerPicture = null;
                    HttpPostedFile manufacturerPictureFile = fuManufacturerPicture.PostedFile;
                    if ((manufacturerPictureFile != null) && (!String.IsNullOrEmpty(manufacturerPictureFile.FileName)))
                    {
                        byte[] manufacturerPictureBinary = PictureManager.GetPictureBits(manufacturerPictureFile.InputStream, manufacturerPictureFile.ContentLength);
                        manufacturerPicture = PictureManager.InsertPicture(manufacturerPictureBinary, manufacturerPictureFile.ContentType, true);
                    }
                    int manufacturerPictureID = 0;
                    if (manufacturerPicture != null)
                        manufacturerPictureID = manufacturerPicture.PictureID;

                    DateTime nowDt = DateTime.Now;
                    Manufacturer manufacturer = ManufacturerManager.InsertManufacturer(txtName.Text, txtDescription.Value, int.Parse(this.ddlTemplate.SelectedItem.Value),
                             txtMetaKeywords.Text, txtMetaDescription.Text, txtMetaTitle.Text, txtSEName.Text,
                            manufacturerPictureID, cbPublished.Checked, false, txtDisplayOrder.Value, nowDt, nowDt);

                    Response.Redirect(string.Format("ManufacturerDetails.aspx?ManufacturerID={0}", manufacturer.ManufacturerID));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        public int ManufacturerID
        {
            get
            {
                return CommonHelper.QueryStringInt("ManufacturerID");
            }
        }
    }
}